import 'package:app/icons/icons.dart';
import 'package:app/main.dart';
import 'package:app/model/app_config.dart';
import 'package:app/model/model.dart';
import 'package:app/page/pageA.dart';
import 'package:app/page/pageB.dart';
import 'package:app/widget/lazy_index_stack.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final LazyIndexStackController controller;
  late final AppLifecycleListener listener;
  late int firstBackTime = 0;
  final cList = CountdownEventClassList.create();
  final List<Widget> pages = const <Widget>[PageA(), PageB()];

  int _index = 0;

  void setCeche() async {
    cList.setCache();

  }

  void getCeche() async {
    cList.getCache();
  }

  @override
  void initState() {
    super.initState();
    controller = LazyIndexStackController(index: _index, children: pages);
    listener = AppLifecycleListener(onDetach: setCeche, onInactive: setCeche);
    getCeche();
  }

  @override
  void dispose() {
    listener.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScopeTestRoute(
      child: Scaffold(
        body: LazyIndexStack(controller: controller),
        bottomNavigationBar: SizedBox(
          height: 60, // Increased height to prevent overflow
          child: BottomNavigationBar(
            type: BottomNavigationBarType.fixed, // Ensure fixed height
            currentIndex: _index,
            iconSize: 25,
            selectedFontSize: 0.0,
            unselectedFontSize: 0.0,
            selectedItemColor: StaticAppConfig.appBarColor,
            onTap: (value) {
              setState(() {
                controller.changeIndex(value);
                _index = value;
              });
            },
            items: const [
              BottomNavigationBarItem(
                icon: Icon(Icons.calendar_month),
                label: "",
              ),
              BottomNavigationBarItem(icon: Icon(AppIcons.book), label: ""),
            ],
          ),
        ),
      ),
    );
  }
}
